package DB.FileCommand;

import Command.AbstractCommand;
import Command.SetCommand;
import Executor.DbThreadExecutor;
import HttpServer.HttpServerClient;
import sun.net.www.http.HttpClient;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class rdb {




   public static void init(){
       DbThreadExecutor.scheduleAtFixedRate(new Runnable() {
           @Override
           public void run() {
               CreateRDB(HttpServerClient.pureLogDb.getNowCache());
           }
       },6,6);
   }


   public static void CreateRDB( ConcurrentHashMap<String,AbstractCommand> DbCache ) {


        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

        BufferedWriter RdbbufferedWriter = null;;
        try {
            RdbbufferedWriter = new BufferedWriter(new FileWriter(new File( formatter.format(calendar.getTime())+"rdb.log")));
            List<String> list = new ArrayList<>();
            for (String key :  DbCache.keySet()){
                SetCommand setCommand = (SetCommand) DbCache.get(key);
                RdbbufferedWriter.write(setCommand.toString());
                RdbbufferedWriter.newLine();
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                RdbbufferedWriter.close();
            }catch (Exception e){
                e.printStackTrace();
            }

        }



    }




}
